class Solution
{
public:
    bool validateBookSequences(vector<int>& putIn, vector<int>& takeOut)
    {
        stack<int> st;
        int n = takeOut.size();
        int k = 0;
        for (int i = 0; i < putIn.size(); i++)
        {
            st.push(putIn[i]);
            while (!st.empty() && k < n && st.top() == takeOut[k])
            {
                st.pop();
                k++;
            }
        }
        return st.empty();
    }
};